package ch5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "IndexServlet",value = "/index")
public class IndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//设置中文编码
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();//寻找session，无则创造
        User user = (User) session.getAttribute("user");
        if (user == null){//判断用户是否登录，若登录，展示首页
            //直接跳转
         //response.sendRedirect("login.html");
            response.getWriter().println("无登录，请<a href='login.html'>登录</a>");
        }else{//无登录，跳登录页
            response.getWriter().println("欢迎登录，"+user.getUsername());
            response.getWriter().println("<a href='logout'>退出</a>");
        }
    }
}
